package com.zhentao.service.impl;

import com.zhentao.domain.SQuestion;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class WordDocumentService {  
  
    public ResponseEntity<Resource> generateWordDocument(List<SQuestion> sQuestions) throws IOException {
        List<String> data = new ArrayList<>();
        int number=0;
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==1) {
                String[] split = sQuestion.getQuestion().split("/");
                data.add((++number)+"."+split[0]);
                String[] s = split[1].split("_");
                data.addAll(Arrays.asList(s));
                data.add("答案："+sQuestion.getAnswerTxt());
                data.add(" ");
            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==2){
                String[] split = sQuestion.getQuestion().split("/");
                data.add((++number)+"."+split[0]);
                String[] s = split[1].split("_");
                data.addAll(Arrays.asList(s));
                data.add("答案："+sQuestion.getAnswerTxt());
                data.add(" ");
            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==3) {

                    data.add((++number)+"."+sQuestion.getQuestion());
                    data.add("答案："+sQuestion.getAnswerTxt());
                    data.add(" ");

            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==4) {
                    data.add((++number)+"."+sQuestion.getQuestion());
                    data.add("答案："+sQuestion.getAnswerTxt());
                    data.add(" ");
            }
        }

        XWPFDocument document = new XWPFDocument();  
  
        for (String item : data) {
            XWPFParagraph paragraph = document.createParagraph();  
            paragraph.createRun().setText(item);  
        }  
  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        document.write(out);  
        document.close();

        HttpHeaders  headers = new HttpHeaders();
        // 创建一个表示当前日期和时间的Date对象
        Date now = new Date();

        // 创建一个SimpleDateFormat对象，用于将Date格式化为年-月-日的字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        // 使用SimpleDateFormat的format方法将Date对象格式化为字符串
        String formattedDate = sdf.format(now);
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+"day"+formattedDate+".docx");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        return ResponseEntity.ok()
                .headers(headers)
                .body(new ByteArrayResource(out.toByteArray()));
    }
    public static boolean containsAllLetters(String str) {
       char[] letters={'A','B','C','D'};
        for (char letter : letters) {
            if (!str.contains(String.valueOf(letter))) {
                return false; // 如果任何一个字母不在字符串中，就返回false
            }
        }
        return true; // 所有字母都在字符串中
    }
}